﻿using Abp.Application.Services;
using Abp.Domain.Repositories;
using Microsoft.AspNetCore.Http;
using SportsPlatform.Base;
using SportsPlatform.Consts;
using SportsPlatform.Dto.SportBusiness.Category;
using SportsPlatform.Dto.WeChat;
using SportsPlatform.Model.SportBusiness;
using SportsPlatform.WeChat.IWeChat;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SportsPlatform.WeChat.WeChat
{
    /// <summary>
    /// 资讯分类服务接口实现
    /// </summary>
    [RemoteService(false)]
    public class CategoryService : SportsPlatformAppServiceBase, ICategoryService
    {
        private readonly IRepository<Category, Guid> _repository;

        public CategoryService(IRepository<Category, Guid> repository, IHttpContextAccessor context) : base(context)
        {
            _repository = repository;
        }

        /// <summary>
        /// 获取所有资讯分类信息列表
        /// </summary>
        /// <returns></returns>
        public async Task<ResultListDto<GetListCategoryDto>> GetCategoryList()
        {
            ResultListDto<GetListCategoryDto> resultDto = new ResultListDto<GetListCategoryDto>();
            try
            {
                var types = await _repository.GetAllListAsync(c => c.ParentId == null && c.IsEnable == true && c.IsDeleted == false);
                if (types.Count > 0)
                {
                    resultDto.Data = ObjectMapper.Map<List<GetListCategoryDto>>(types);
                }
                resultDto.Code = types.Count > 0 ? HttpResultCode.Success : HttpResultCode.SqlError;
                resultDto.IsSuccess = types.Count > 0;
                resultDto.Message = types.Count > 0 ? "获取成功" : "获取失败";
            }
            catch (Exception e)
            {
                resultDto.Code = HttpResultCode.Error;
                resultDto.IsSuccess = false;
                resultDto.Message = e.Message;
            }
            return resultDto;
        }
    }
}
